import 'babel-polyfill';
const STATUS_CODE ={
    ADMIN:{id:1000,note:'超级管理员',key:'ADMIN'},
    MANAGER:{id:2000,note:'管理员',key:'MANAGER'},
    NORMAL:{id:4000,note:'普通用户',key:'NORMAL'},
    GUESS:{id:5000,note:'访客',key:'GUESS'},
    EMPTY:{id:6000,note:'空用户',key:'EMPTY'},
};
const state ={
    username:'',
    userpassword:'',
    userstatus:STATUS_CODE.GUESS.key,
    guesscount:0,

};

const mutations ={
    increment(state,addend){
        console.log(addend);
        if(typeof addend !== 'number')
            addend = 0;
        state.guesscount += addend;
    },
    setguesscount(state,count){
        if(typeof  count === 'number')
            state.guesscount = count;
        return count;
    }
};

const actions = {
    increment(context,addend){
        console.log('action');
        context.commit('increment',addend);
    },
    setguesscount({commit},count){
        commit('setguesscount',count);
        return 'il';
    },
    async incrementAsync({commit}){
        setTimeout(()=>{
            commit('increment',5);
            return 'async';
        },1000);
    },
    async incrementPromise({commit}){
        return new Promise((resolve,reject)=>{
            setTimeout(()=>{
                commit('increment',10);
                resolve(101);
            },1000);
        });
    }
};

const getters = {
    statuscode:state=>{
        return STATUS_CODE[state.userstatus].id;
    },
    statusnote:state=>{
        return STATUS_CODE[state.userstatus].note;
        //return state[state.userstatus].note;
    },
    getStatus:state=>{
        return (key)=>{
            return  STATUS_CODE[key]?STATUS_CODE[key]:STATUS_CODE.EMPTY;
        }
    }
};
export default {
    namespaced:true,
    state,
    mutations,
    actions,
    getters
}